<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Level extends JS_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model('level_mdl');
		if($this->session->suser->group != 1){
			show_error('参数错误！');
		}
	}

	public function index(){
		$this->db->order_by('hierarchy asc');
		$list = $this->level_mdl->select();
		$list = $this->level_mdl->reset($list);
		$data = array(
			'list' => $list
			);
		$this->_view('level_index',$data);
	}

	/**
	 * 新增等级
	 */
	public function create(){
		//查询最高层级
		$res_max = $this->level_mdl->get_max();
		$data = array(
			'max' => $res_max['hierarchy']+1
		);
		$this->_view('level_create',$data);
	}

	/**
	 * 修改等级
	 */
	public function update(){
		$oid = $this->input->get('oid');
		$oid = intval($oid);

		if(empty($oid)){
			show_error('参数错误！');
		}

		$this->db->where('id',$oid);
		$res = $this->level_mdl->get_one();

		if(empty($res)){
			show_error('该数据不存在');
		}

		$this->_view('level_update',['res' => $res]);
	}

	/**
	 * 等级操作
	 */
	public function operation(){
		if(!$this->input->is_ajax_request()){
			show_404();
		}

		$post = $this->input->post(NULL,TRUE);
		if(empty($post)){
			show_error('参数错误！');
		}

		if(empty($post['name'])){
			die('等级名称不能为空');
		}

		if(!is_numeric($post['prepaid'])){
			die('预付金额不能为空，必须为大于零的整数');
		}

		if(!is_numeric($post['num']) || $post['num'] < 0){
			die('配送店铺必须为整数');
		}

		if(empty($post['hierarchy'])){
			die('等级层级是金额运算的关键，不能为空，建议保持系统默认');
		}

		if(!is_numeric($post['hierarchy']) || $post['hierarchy'] < 0){
			die('等级层级为大于零的整数，建议保持系统默认');
		}

		if(!is_numeric($post['cost']) || $post['cost'] < 0){
			die('店铺成本不能为空，必须为大于零的整数');
		}

		if(!is_numeric($post['commission']) || $post['commission'] < 0){
			die('单笔提成为正整数，省去“%”');
		}

		if(!is_numeric($post['max']) || $post['commission'] < 0){
			die('最大利润比为正整数，省去“%”');
		}

		if(!is_numeric($post['min']) || $post['commission'] < 0){
			die('最小利润比为正整数，省去“%”');
		}

		if(!is_numeric($post['group_id'])){
			die('请输入正确的权限编号，没有则为0');
		}

		$data = array(
			'name' => $post['name'],
			'prepaid' => intval($post['prepaid']),  
			'num' => intval($post['num']),  
			'power' => $post['power'],  
			'note' => $post['note'],  
			'hierarchy' => $post['hierarchy'],  
			'commission' => $post['commission'] / 100,  
			'max' => $post['max'] / 100 ,  
			'min' => $post['min'] / 100,
			'cost' => $post['cost'],
			'group_id' => intval($post['group_id'])
		); 

		/** 判断修改还是新增 */
		$this->db->select('id');
		$this->db->where('hierarchy',$post['hierarchy']);
		$res_one = $this->level_mdl->get_one();

		if(!empty($post['oid'])){
			if($res_one['id'] != $post['oid']){
				die('修改失败！参数错误！');
			}
			$this->db->where('id',intval($post['oid']));
			$res = $this->level_mdl->update($data);
		}else{
			if(!empty($res_one)){
				die('该层级已有数据存在，建议保持系统默认');
			}
			$res = $this->level_mdl->create($data);
		}

		echo $res === true ? 1 : '操作失败！';

	}

}

/* End of file Level.php */
/* Location: ./application/controllers/Level.php */